package scau.cxq.homework.service.impl;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import scau.cxq.homework.dao.CaixiaoqingResultMapper;
import scau.cxq.homework.dto.LogExecuteDto;
import scau.cxq.homework.pojo.CaixiaoqingResult;
import scau.cxq.homework.pojo.CaixiaoqingResultExample;
import scau.cxq.homework.service.CaixiaoqingResultService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class CaixiaoqingResultServiceImpl implements CaixiaoqingResultService {


    @Autowired
    private CaixiaoqingResultMapper caixiaoqingResultMapper;

    @Override
    public List<CaixiaoqingResult> selectAllCaixiaoqingResult() {

        CaixiaoqingResultExample caixiaoqingResultExample = new CaixiaoqingResultExample();

        return caixiaoqingResultMapper.selectByExample(caixiaoqingResultExample);
    }

    @Override
    public LogExecuteDto getLogExecuteDto() {

        CaixiaoqingResultExample example = new CaixiaoqingResultExample();
        example.setOrderByClause("dateStr ASC");

        List<CaixiaoqingResult> results = caixiaoqingResultMapper.selectByExample(example);

        LogExecuteDto logExecuteDto = new LogExecuteDto();

        List<String> dates = new ArrayList<>();
        List<BigDecimal> data = new ArrayList<>();

        for (CaixiaoqingResult result : results) {
            dates.add(result.getDatestr());
            data.add(result.getAvgpvnum());
        }

        logExecuteDto.setDates(dates);
        logExecuteDto.setData(data);

        return logExecuteDto;
    }
}
